package com.kl.forum.service;

import com.kl.forum.dao.MemberMapper;
import com.kl.forum.dao.OperationRecordMapper;
import com.kl.forum.dao.SignConfigMapper;
import com.kl.forum.dao.SignMapper;
import com.kl.forum.entity.Member;
import com.kl.forum.entity.OperationRecord;
import com.kl.forum.entity.Sign;
import com.kl.forum.entity.SignConfig;
import com.kl.forum.enums.ResultCode;
import com.kl.forum.util.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import static com.kl.forum.util.DateUtil.differentDays;

@Service
public class SigInService {
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private SignConfigMapper signConfigMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OperationRecordMapper operationRecordMapper;

    public ResultInfo signIn(Integer userId) {
        ResultInfo re=new ResultInfo();
        Member member= memberMapper.findUserByUserId(userId);
        SignConfig signConfig= signConfigMapper.selectByPrimaryKey(1);
        try {
            Sign sign1= signMapper.findSignByUserId(userId);
            if (sign1==null){
                Sign sign=new Sign();
                sign.setUserId(userId);
                sign.setUpdateTime(new Date());
                sign.setCreateTime(new Date());
                sign.setStatus(1);
                sign.setDayNum(1);
                addOperationRecord(userId,"+"+signConfig.getGold1()+"金币");
                member.setCoinNumber(member.getCoinNumber()+signConfig.getGold1());
                member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold1());
                memberMapper.updateByPrimaryKeySelective(member);
                signMapper.insertSelective(sign);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else {
                if (differentDays(sign1.getUpdateTime(),new Date())==0){
                    re.setMessage("已签到");
                    re.setCode("01");
                    return  re;
                }

                if (sign1.getDayNum()>=7||differentDays(sign1.getUpdateTime(),new Date())>1){
                    addOperationRecord(userId,"+"+signConfig.getGold1()+"金币");
                    sign1.setDayNum(1);
                    sign1.setUpdateTime(new Date());
                    member.setCoinNumber(member.getCoinNumber()+signConfig.getGold1());
                    member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold1());
                    memberMapper.updateByPrimaryKeySelective(member);
                    signMapper.updateByPrimaryKeySelective(sign1);
                    re.setMessage(ResultCode.SUCCESS.getMsg());
                    re.setCode(ResultCode.SUCCESS.getCode());
                    return  re;
                }
                if (sign1.getDayNum()==0){
                    addOperationRecord(userId,"+"+signConfig.getGold1()+"金币");
                    member.setCoinNumber(member.getCoinNumber()+signConfig.getGold1());
                    member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold1());
                    memberMapper.updateByPrimaryKeySelective(member);
                }
                    if (sign1.getDayNum()==1){
                        addOperationRecord(userId,"+"+signConfig.getGold2()+"金币");
                        member.setCoinNumber(member.getCoinNumber()+signConfig.getGold2());
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold2());
                        memberMapper.updateByPrimaryKeySelective(member);
                    }
                    if (sign1.getDayNum()==2){
                        addOperationRecord(userId,"+"+signConfig.getGold3()+"金币");
                        sign1.setStatus(0);
                        member.setCoinNumber(member.getCoinNumber()+signConfig.getGold3());
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold3());
                    }
                    if (sign1.getDayNum()==3){
                        addOperationRecord(userId,"+"+signConfig.getGold4()+"金币");
                        member.setCoinNumber(member.getCoinNumber()+signConfig.getGold4());
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold4());
                    }
                    if (sign1.getDayNum()==4){
                        addOperationRecord(userId,"+"+signConfig.getGold5()+"金币");
                        member.setCoinNumber(member.getCoinNumber()+signConfig.getGold5());
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold5());
                    }
                    if (sign1.getDayNum()==5){
                        addOperationRecord(userId,"+"+signConfig.getGold6()+"金币");
                        member.setCoinNumber(member.getCoinNumber()+signConfig.getGold6());
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+signConfig.getGold6());
                    }
                    if (sign1.getDayNum()==6){
                        int max=signConfig.getGoldMax();
                        int min=signConfig.getGoldMin();
                        Random rand = new Random();
                        int gold7=rand.nextInt(max - min + 1) + min;
                        addOperationRecord(userId,"+"+gold7+"金币");
                        member.setCoinNumber(member.getCoinNumber()+gold7);
                        member.setAccumulativeCoin(member.getAccumulativeCoin()+gold7);
                        re.setData(gold7);
                    }
                    sign1.setDayNum(sign1.getDayNum()+1);
                    sign1.setUpdateTime(new Date());
                memberMapper.updateByPrimaryKeySelective(member);
                signMapper.updateByPrimaryKeySelective(sign1);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setData("系统异常");
        re.setMessage(ResultCode.SUCCESS.getMsg());
        re.setCode(ResultCode.SUCCESS.getCode());
        return  re;
    }

    /**
     * 收益记录
     * @return
     */
    public void addOperationRecord(Integer userId,String number){
        OperationRecord record=new OperationRecord();
        //type=2 =签到
        record.setName("签到");
        record.setType(2);
        record.setNumber(number);
        record.setStatus(2);
        record.setUserId(userId);
        record.setCreateTime(new Date());
        record.setReflectType(0);
        record.setTransactionTime(new Date());
        operationRecordMapper.insertSelective(record);
    }
    public ResultInfo signInPage(Integer userId) {
        ResultInfo re=new ResultInfo();
        Map<String,Object> map=new HashMap<>();
        try {
            Sign sign=signMapper.findSignByUserId(userId);
            if (sign!=null){
                if (differentDays(sign.getUpdateTime(),new Date())>1){
                    sign.setDayNum(0);
                    signMapper.updateByPrimaryKeySelective(sign);
                }
                int day=differentDays(sign.getUpdateTime(),new Date());
                if (sign.getDayNum()+day>7){
                    sign.setDayNum(0);
                    signMapper.updateByPrimaryKeySelective(sign);
                }
            }
                SignConfig config=signConfigMapper.selectByPrimaryKey(1);
                map.put("sign",sign);
                map.put("config",config);
            re.setData(map);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.SUCCESS.getMsg());
        re.setCode(ResultCode.SUCCESS.getCode());
        return  re;
    }

    public ResultInfo signInStatus(Integer userId) {
        ResultInfo re= new ResultInfo();
        try {
            Sign sign=signMapper.findSignByUserId(userId);
            if (sign==null){
                re.setMessage("未签到");
                re.setCode("02");
                return  re;
            }
            if (differentDays(sign.getUpdateTime(),new Date())==0){
                re.setMessage("已签到");
                re.setCode("01");
                return  re;
            }
            if (differentDays(sign.getUpdateTime(),new Date())>0){
                re.setMessage("未签到");
                re.setCode("02");
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.SUCCESS.getMsg());
        re.setCode(ResultCode.SUCCESS.getCode());
        return  re;
    }

    public ResultInfo findUser(Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            Member member= memberMapper.findUserByUserId(userId);
            re.setData(member);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setData("系统错误");
        re.setMessage(ResultCode.SUCCESS.getMsg());
        re.setCode(ResultCode.SUCCESS.getCode());
        return  re;
    }
}
